package com.xbe.equipment.controller;

import com.xbe.common.annotation.Log;
import com.xbe.common.core.controller.BaseController;
import com.xbe.common.core.domain.AjaxResult;
import com.xbe.common.core.page.TableDataInfo;
import com.xbe.common.enums.BusinessType;
import com.xbe.common.utils.poi.ExcelUtil;
import com.xbe.equipment.domain.Equipment;
import com.xbe.equipment.service.IEquipmentService;
import com.xbe.framework.util.ShiroUtils;
import com.xbe.order.domain.Order;
import com.xbe.order.service.IOrderService;
import com.xbe.road.domain.Road;
import com.xbe.road.service.IRoadService;
import com.xbe.system.domain.SysUser;
import com.xbe.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备管理Controller
 *
 * @author liuzhiting
 * @date 2020-06-19
 */
@Controller
@RequestMapping("/equipment/equipment")
public class EquipmentController extends BaseController
{
    private String prefix = "equipment/equipment";

    @Autowired
    private IEquipmentService equipmentService;

    @Autowired
    private IRoadService roadService;

    @Autowired
    private ISysUserService userService;


    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("equipment:equipment:view")
    @GetMapping()
    public String equipment(ModelMap map)
    {
        List<SysUser> users = userService.selectUserAll();

        List list = new ArrayList();
        for (SysUser user : users)
        {
            Map userMap = new HashMap();
            userMap.put("value", user.getUserName());
            userMap.put("text", user.getUserName());
            list.add(userMap);
        }
        map.put("users", list);
        return prefix + "/equipment";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("equipment:equipment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Equipment equipment)
    {
        startPage();
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        String loginName = ShiroUtils.getLoginName();

        if ("admin".equals(loginName) || "admin" == loginName)
        {
            List<Equipment> listAll = equipmentService.selectEquipmentListAll(equipment);
            return getDataTable(listAll);
        }
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @RequiresPermissions("equipment:equipment:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Equipment equipment)
    {
        List<Equipment> list = equipmentService.selectEquipmentList(equipment);
        ExcelUtil<Equipment> util = new ExcelUtil<Equipment>(Equipment.class);
        return util.exportExcel(list, "equipment");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("equipment:equipment:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Equipment equipment)
    {
        return toAjax(equipmentService.insertEquipment(equipment));
    }

    /**
     * 修改设备管理  -----设备控制
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("equipment:equipment:control")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Equipment equipment = equipmentService.selectEquipmentById(id);
        mmap.put("equipment", equipment);
        List<SysUser> sysUsers = userService.selectUserAll();
        mmap.put("users", sysUsers);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理
     */
    // @RequiresPermissions("equipment:equipment:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Equipment equipment)
    {
        Road road = roadService.selectRoadById(Long.valueOf(equipment.getRoadId()));
        equipment.setRoadName(road.getName());
        return toAjax(equipmentService.updateEquipment(equipment));
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("equipment:equipment:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(equipmentService.deleteEquipmentByIds(ids));
    }

    //修改道路
    @GetMapping("/updateRoad/{id}")
    public String updateRoad(@PathVariable("id") Long id, ModelMap mmap)
    {

        List<Road> roads = roadService.selectRoadList(null);
        Equipment equipment = equipmentService.selectEquipmentById(id);

        for (Road road : roads)
        {
            if (equipment.getRoadId() != null)
            {
                if (road.getId().longValue() == equipment.getRoadId().longValue())
                {
                    road.setRoadflag(true);
                    break;
                }
            }

        }


        mmap.put("roads", roads);
        mmap.put("id", id);
        mmap.put("name", equipment.getName());

        return prefix + "/updateRoad";
    }

    //修改道路
    @RequiresPermissions("equipment:equipment:updateRoad")
    @PostMapping("/updateRoad")
    @ResponseBody
    public String doUpdateRoad(Equipment equipment)
    {

        return prefix + "/updateRoad";
    }

    //修改设备名称
    @RequiresPermissions("equipment:equipment:updateEquipmentName")
    @PostMapping("/editEquimentName/{id}/{name}")
    @ResponseBody
    public AjaxResult editEquimentName(@PathVariable("id") Long id,
                                       @PathVariable("name") String name)
    {

        Equipment equipment = new Equipment();
        equipment.setId(id);
        equipment.setName(name);


        return toAjax(equipmentService.updateEquipment(equipment));
    }

    //查看命令集合
    @GetMapping("/lookOrders/{sim}")
    public String lookOrders(@PathVariable("sim") String sim, ModelMap map)
    {
        map.put("sim", sim);
        return prefix + "/orderList";
    }

    @PostMapping("/lookOrders/{sim}")
    @ResponseBody
    public TableDataInfo lookOrders(@PathVariable("sim") String sim)
    {


        startPage();
        Order order = new Order();
        order.setSim(sim);
        List<Order> orders = orderService.selectOrderList(order);
        return getDataTable(orders);
    }


}
